package com.aci.test.wordfreq.io;

import java.io.PrintStream;
import java.util.Iterator;

import com.aci.test.wordfreq.sort.IWordsListSelector;
import com.aci.test.wordfreq.sort.IWordsSorter;

public class SelectionPrinter {

	private IWordsSorter sorter;
	private IWordsListSelector selector;

	public SelectionPrinter(IWordsSorter sorter, IWordsListSelector selector) {
		if (null == sorter) {
			throw new IllegalArgumentException("sorter cannot be null");
		} else if (null == selector) {
			throw new IllegalArgumentException("selector cannot be null");
		}

		this.sorter = sorter;
		this.selector = selector;
	}

	public void printSelected(Iterator<String> words, PrintStream printStream) {
		// sort and select words
		Iterable<String> sortedWords = sorter.sort(words);
		Iterable<String> selectedWords = selector.select(sortedWords);
		// print selection
		for (String selectedWord : selectedWords) {
			printStream.println(selectedWord);
		}
	}

}
